//
//  UserDefaults.m
//  s.
//
//  Created by pro on 2021/1/19.
//  Copyright © 2021 hailuo. All rights reserved.
//

#import "UserDefaults.h"

@implementation UserDefaults
#pragma mark - DriverModel
/* 保存DriverModel */
+(void)saveUserModel:(UserModel *)userModel;
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *dogPath = [docPath stringByAppendingPathComponent:@"lightthingssaveUserModel"];
    [NSKeyedArchiver archiveRootObject:userModel toFile:dogPath];
}
/* 取出DriverModel */
+(UserModel *)fetchUserModel;
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:@"lightthingssaveUserModel"];
    UserModel *userModel  =[NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return userModel;
}


/* 清空DriverModel **/
+(void)removeUserModel {
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:@"lightthingssaveUserModel"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSError *error = nil;
        [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
        if (error) {
            JYLog(@"清除用户信息失败");
        } else {
            JYLog(@"清除用户信息成功");
        }
     }
}

+ (NSDictionary *)fetchDefaultAddress {
    return  [[NSUserDefaults standardUserDefaults] valueForKey:@"NP.defaultAddress"];
}
+ (void)saveDefaultAddress:(NSDictionary *)address {
    
    
    
    [[NSUserDefaults standardUserDefaults] setObject:address forKey:@"NP.defaultAddress"];
}


+(void)removeUserBalance {
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:@"lightthingsbalance"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSError *error = nil;
        [[NSFileManager defaultManager] removeItemAtPath:path error:&error];

     }
}


+ (NSArray *)fetchMoneyEnum {
    
    NSString * currentlanguage = [UserDefaults fetchLanguage];
    if ([currentlanguage hasPrefix:@"my"]) {
        //老挝语
        return @[@"ဒေါ်လာ", @"ယွမ်​", @"ဘတ်",@"ကျပ်​", @"ဒေါ်လာ"];
    }else if ([currentlanguage hasPrefix:@"zh"]){
        //中文
        return @[@"美元", @"元", @"泰铢",@"缅甸元", @"美元"];
    }else{
        //英语
        return @[@"Dollar", @"Yuan", @"Baht",@"Ks", @"Dollar"];
    }
    
}
+ (NSString *)fetchMoneyByEnum:(int)Enum {
    return [self fetchMoneyEnum][Enum];
}
+ (int)fetchMoneyEnumbyText:(NSString *)Enum{
    return [[self fetchMoneyEnum] indexOfObject:Enum];
}

+ (int)fetchMoneyEnumValue {
    return [[[NSUserDefaults standardUserDefaults] objectForKey:@"U.unit"] intValue];
}

+ (void)saveMoneyEnumValue:(int)value {
    [[NSUserDefaults standardUserDefaults] setObject:@(value) forKey:@"U.unit"];
}

+ (NSString *)fetchMoneyType:(int)type {
    return @[@"USD", @"CNY", @"THB", @"MMK", @"USDT"][type];
}

+ (NSString *)fetchCurrencySymbol{
    return [self fetchSymbol:[UserDefaults fetchMoneyEnumValue]];
}

+ (NSString *)fetchSymbol:(int)type {
    NSArray * list = @[@"$", @"¥", @"฿", @"Ks", @"$"];
    return list[type];
}

+(void)savePayPassword:(NSString *)unit{
    [[NSUserDefaults standardUserDefaults] setObject:unit forKey:@"U.paypassword"];
}
+(NSString *)fetchPayPassword {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"U.paypassword"];;
}



/* 保存时间间隔 */
+(void)saveToken:(NSString *)IntervalNum
{
    [[NSUserDefaults standardUserDefaults] setObject:IntervalNum forKey:@"U.token"];
}
/* 取出时间间隔 */
+(NSString *)fetchToken;
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"U.token"];;
}

+(void)saveLanguage:(NSString *)language{
    [[NSUserDefaults standardUserDefaults] setObject:language forKey:@"U.language"];

}
+(NSString *)fetchLanguage {
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"U.language"]) {
        return  [[NSUserDefaults standardUserDefaults] objectForKey:@"U.language"];
    }
    return @"zh_CN";
}

//+(void)saveUnit:(NSString *)unit {
//    [[NSUserDefaults standardUserDefaults] setObject:unit forKey:@"U.unit"];
//}
//+(NSString *)fetchUnit{
//    return [[NSUserDefaults standardUserDefaults] objectForKey:@"U.unit"];;
//}
+ (BOOL)fetchVip{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:@"lightthingsvip"] boolValue];
}

+ (void)clearLocalInfo {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"X-ACCESS-TOKEN"];
  
    [UserDefaults removeUserModel];
    [UserDefaults removeUserBalance];
}

#pragma mark - ConfigDic
+(void)saveConfigDic:(NSDictionary *)configDic;
{

    [[NSUserDefaults standardUserDefaults] setObject:[configDic mj_JSONData] forKey:@"shop.saveConfigDic"];
}

+(NSDictionary *)fetchConfigDic;
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"shop.saveConfigDic"];
    NSDictionary *configDic = [data mj_JSONObject];
    return configDic;
}


@end
